using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(SpriteRenderer))]
[RequireComponent(typeof(Collider2D))]
public class PerkPickup:ItemPickup {
	override protected void Start() {
		base.Start();
		spriteRenderer.sprite=Perk.perkData[perkId].sprite;
	}
	private void SetId(int id) {
		if(id>=Perk.perkCount) {
			id=0;
			Debug.LogWarning("Trying to set a pickup to an id that is out of range.");
		}
		perkId=id;
		if(spriteRenderer!=null) spriteRenderer.sprite=Perk.perkData[id].sprite;
	}

	static GameObject pickablePrefab;
	public static PerkPickup Create(Vector2 position,int id) {
		if(pickablePrefab==null) pickablePrefab=Resources.Load<GameObject>("PerkPickup");
		GameObject gameObject = Instantiate(pickablePrefab,position,Quaternion.identity,Utility.dynamicParent.transform);
		PerkPickup pickup = gameObject.GetComponentInChildren<PerkPickup>();
		pickup.SetId(id);
		return pickup;
	}

	public int perkId { get; private set; }
	protected override void HandleInteract() {
		if(!CostPrice(Player.instance)) return;
		if(Perk.Create(perkId)!=null) Consume();
	}

	public override string GetDescr() { return Perk.perkData[perkId].description; }
	public override string GetName() { return Perk.perkData[perkId].name; }
}